<?php

/**
 * @author nhatnam
 * @copyright 2011
 */
//tao function ket noi
include ('clsketnoi.php');

function taomatheloai()
{
    return taomatudong("select matl from tbltheloai","matl",2,10);
}

//them du lieu
function themtheloai()
{
    $matl = $_POST['matl'];
    $tentl = $_POST['tentl'];
    $vitri = $_POST['vitri'];
    
    mysql_query("insert into tbltheloai (matl, tentl, vitri) values ('$matl','$tentl','$vitri')",dbketnoi());
    mysql_close();
}
//xoa the loai cac dau sach lien quan
function xoatheloai()
{
    $matl= $_POST['matl'];
    $query = mysql_query("select madausach, matl from tbldausach",dbketnoi());
    $i=0;
    while($result = mysql_fetch_array($query))
    {
        if($result['matl']==$matl)
        {
            mysql_query("delete from tbldausach where madausach='".$result['madausach']."'", dbketnoi());
        }
        $i++;
    }
    
    
    mysql_query("delete from tbltheloai where matl='$matl'",dbketnoi());
    mysql_close();
}
//xoa ben dau sach

//update tac gia
function updatetheloai()
{
    $matl = $_POST['matl'];
    $tentl = $_POST['tentl'];
    $vitri = $_POST['vitri'];
    
    mysql_query("update tbltheloai set tentl='$tentl', vitri='$vitri' where matl='$matl'",dbketnoi());
    mysql_close();
}

// hien thi du lieu
function hienthitheloai()
{
    $limit = 15; //gioi han so row trong 1 trang

    $page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1; //get trang hien thi

    $page_start = ( $page - 1 ) * $limit;
    $page_end = $page * $limit;

    $query = mysql_query("SELECT matl, tentl, vitri from tbltheloai",dbketnoi());

    $number_of_page = ceil(mysql_num_rows($query)/$limit); // lay tong row

    if ($number_of_page > 1)
    {
        $list_page = " <td> Trang: </td>";

        for ( $i = 1; $i <= $number_of_page; $i++ )
            {
            if ( $i == $page )
            {
                $list_page .= " <td>[ <b>{$i}</b> ]</td> ";
           
            }
            else
            {
                $list_page .= "<td><a href='giaodientheloai.php?page={$i}'> {$i} </a></td>";
                
            }
        }
    }
    taotable();
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        if ( $i >= $page_start )
        {
            //mau bg cho dong chan le
            if($i%2==0)
                $bg = "#d0eafb";
            else
                $bg = "#66aee0";
            
            //tao ra 1 table bang lenh ket hop css
            echo "<tr bgcolor='".$bg."' >"; 
            echo "<td><a href='' >".$result['matl']."</a></td>";
            echo "<td>".$result['tentl']."</td>";
            echo "<td>".$result['vitri']."</td>";
            echo "</tr>";
        }

        $i++;

        if ($i >= $page_end)
        {
            break;
        }
    }
    echo "</table></br>";
    echo $list_page;
}
function taotable()
{
    $title = array("Mã Thể Loại ","Tên Thể Loại","Vị Trí");
    echo "<table style='border-color:black;'  cellspacing='1' width='500'>";
    echo "<tr>";
    for($i=0; $i<=2; $i++)
    {
        echo "<th style='background-color:#2287cd; color='white';>".$title[$i]."</th>"; 
    }
    echo "</tr>";
  
}

?>
